<?php
include '../_mysql.php';
include '../_settings.php';
include '../_functions.php';

$limit = 10;
$id = $_GET['id'];
$page = $_GET['page'];
$start = ($page - 1) * $limit;
$gig = safe_query("SELECT item.id, title, content, price, category, image, owner, rating.rating_min, rating.rating_max FROM item INNER JOIN rating ON item.id = rating.id WHERE item.id='".$id."'");
$reviews = safe_query("SELECT * FROM reviews WHERE gig='".$id."' LIMIT ".$start .", " . ($limit-1));

$reviews_data = '';
if(mysql_num_rows($gig))
{	 
	$ds = mysql_fetch_array($gig);
	$owner = username($ds['owner']);
	$category = category($ds['category']);
	
	//----Reviews----
	$r_count = mysql_num_rows($reviews);
	$r_positive = mysql_num_rows(safe_query("SELECT rating FROM reviews WHERE gig='".$id."' AND rating=1"));
	$r_negative = mysql_num_rows(safe_query("SELECT rating FROM reviews WHERE gig='".$id."' AND rating=0"));
	if($r_count)
	{
		while($review = mysql_fetch_array($reviews))
		{
			$rating = 'negative';
			if($review['rating'])
				$rating = 'positive';
			$user = username($review['user']);
			
			$reply_q = "SELECT * FROM reviews WHERE gig='".$id."' AND reply='".$review['id']."'";
			if(mysql_num_rows(safe_query($reply_q)))
			{
				$reply = mysql_fetch_array(safe_query($reply_q));
				$reply_user = username($reply['user']);
				
				?>
				<li class="review-item">
				<div class="comment-wrap clearafter">
					<span class="$rating"></span>
					<img class="left member-avata" src="images/avata.png" />
					<div class="left review-detail">
						<a href="#" class="member-username"><?=$user?></a>
						<span class="time">4 days ago</span>
						<p><?=$review[content]?></p>
					</div>
				</div>
				<ul class="child">
					<li class="review-item">
						<div class="comment-wrap clearafter">
							<span class="$rating"></span>
							<img class="left member-avata" src="images/avata.png" />
							<div class="left review-detail">
								<a href="#" class="member-username"><?=$reply_user?></a>
								<span class="time">4 days ago</span>
								<p><?=$reply[content]?></p>
							</div>
						</div>
					</li>
				</ul>
				
				<?php
			}
			else
			{
				if($review['reply'] == 0)
				{
					?>
					<li class="review-item">
					<div class="comment-wrap clearafter">
						<span class="$rating"></span>
						<img class="left member-avata" src="images/avata.png" />
						<div class="left review-detail">
							<a href="#" class="member-username"><?=$user?></a>
							<span class="time">4 days ago</span>
							<p><?=$review[content]?></p>
						</div>
					</div>
					<?php
				}
			}
		}
		$load = '<a href="#" class="more-review">Load more</a>';
	}
	
}
else //Gig does not exist
{
redirect('index.php', '', 0);
}
?>